package com.jt.mall.service.impl;

import com.alibaba.fastjson.JSON;
import com.jt.mall.common.po.ItemCat;
import com.jt.mall.mapper.ItemCatMapper;
import com.jt.mall.service.ItemCatService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wangning
 * @create 2021-12-27 15:52
 */
@Service
public class ItemCatServiceImpl implements ItemCatService {
	@Autowired
	private ItemCatMapper itemCatMapper;

	@Autowired
	private RedisTemplate redisTemplate;

	@Override
	public List<ItemCat> findItemCatById(Long id) {
		return itemCatMapper.findItemCatById(id);
	}

	@Override
	public List<ItemCat> findItemCatByParentId(Long parentId) {
		List<ItemCat> itemCatList = itemCatMapper.findItemCatByParentId(parentId);
		String key = "ITEM_CAT_00" + parentId;
//		String key = "test01";
//		String value = JSON.toJSONString(itemCatList);
//		System.out.println("value = " + value);
//		ValueOperations<String, String> operation = redisTemplate.opsForValue();
//		operation.set(key, value);
		ListOperations listOperation = redisTemplate.opsForList();
		if (null != itemCatList)
		{
			int size = itemCatList.size();
			for (int i = 0; i < size; i++)
			{
				listOperation.leftPush(key, itemCatList.get(i));
			}
		}
		redisTemplate.opsForList().leftPush(key, itemCatList);
		return itemCatList;
	}

}
